package com.doctor.controller.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.doctor.common.Constat;
import com.doctor.common.JsonResult;
import com.doctor.controller.BaseController;
import com.doctor.entity.Case;
import com.doctor.service.CaseService;

@Controller
@RequestMapping("/admin/case")
public class CaseController extends BaseController {

	@Resource
	private CaseService caseService;

	@GetMapping("/index")
	public String index() {
		return "admin/case/index";
	}

	@GetMapping("/view")
	public String view(ModelMap map) {
		List<Case> list = caseService.findAllMan();
		map.put("list", list);
		return "admin/case/view";
	}

	@PostMapping("/list")
	@ResponseBody
	public Page<Case> list() {
		PageRequest pageRequest = getPageRequest();
		Page<Case> page = caseService.findAll(pageRequest);
		return page;
	}

	@GetMapping("/form")
	public String form(@RequestParam(required = false) Long id, ModelMap map) {
		map.put("cmap", Constat.CASE_MAP);
		if (id != null) {
			Case tbcase = caseService.findById(id);
			map.put("case", tbcase);
		}
		return "admin/case/form";
	}

	@PostMapping("/save")
	@ResponseBody
	public JsonResult save(Case tbcase) {
		try {
			if (tbcase != null) {
				caseService.saveOrUpdate(tbcase);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.fail(e.getMessage());
		}
		return JsonResult.ok();
	}

	@PostMapping("/{id}/del")
	@ResponseBody
	public JsonResult delete(@PathVariable Long id) {
		try {
			caseService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.fail(e.getMessage());
		}
		return JsonResult.ok();
	}

}
